<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib uri="token-tag" prefix="t"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<jsp:include page="/common/commonCss.jsp"></jsp:include>
<jsp:include page="/common/commonJs.jsp"></jsp:include>
<script type="text/javascript"
	src="${basePath }commonJs/quinn_commons.js"></script>
<script type="text/javascript">
	//当前的数量
	var currentCount = null;

	var isZdzk = false;

	var zdzk_value = 1;

	var zdzk_id = null;

	var manager = null;

	$(document).ready(function() {
		changeAllPrice();
	});

	var discountMap = new HashMap();

	function openWindow(oid) {
		$("#orderPrdId").val(oid);
		$('#e').window('open');
	}

	function closeWindow() {
		$('#e').window('close');
		$("#discount_select option:selected").attr("selected", false);
		$("#orderPrdId").val(null);
		$("#mg_password").val(null);
		$("#discount_select").val(null);
		isZdzk = false;
	}

	function zddz() {
		isZdzk = true;
		$('#e').window('open');
	}

	function executeDiscount() {

		manager = null;

		var password = $("#mg_password").val();
		var disc = $("#discount_select").val();
		if (disc.length == 0) {
			alertInfo("请选择折扣");
			return;
		}
		var txt = $("#discount_select option:selected").text();

		var orderPrdId = $("#orderPrdId").val();

		$.ajax({
			url : "${basePath}common/validateManager.shtml",
			data : {
				password : password
			},
			type : "post",
			dataType : "json",
			async : false,
			success : function(data) {
				if (data != null) {
					manager = data.pkUsrId;
				}
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});

		if (manager == null) {
			alertInfo("密码输入错误或不存在!");
			return;
		}

		var v_i = disc.split("_");

		if (isZdzk) {
			$("#zdzk").html(txt);
			zdzk_value = parseFloat(v_i[0]);
			zdzk_id = v_i[1];
			$("#table1 tr:not(:first,:last)").each(
					function() {
						$(this).find("td:eq(5) span").html("-");
						$(this).find("td:eq(7)").html(
								$(this).find("td:eq(4) span").html());
					});
			$("#fkDisId").val(zdzk_id);
			changeAllPrice();
			discountMap = new HashMap();
		} else {

			$("#fkDisId").val(null);
			var dis = {};
			dis.id = v_i[1];
			dis.value = v_i[0];

			$("#span_" + orderPrdId).html(txt);
			$("#zdzk").html("-");
			$("#zkje").html("0");
			zdzk_value = 1;

			discountMap.put(orderPrdId, dis);
			changeDiscountPrice(orderPrdId);
			changeAllPrice();
		}
		closeWindow();
	}

	function changeDiscountPrice(id) {
		var o = discountMap.get(id);
		var zkl = o == null ? 1 : parseFloat(o.value);
		var xj = $("#span_xj_" + id).html();
		var v = parseFloat(xj) * zkl;
		$("#tr_" + id).find("td:eq(7)").html(v.toFixed(2));
	}

	function checkCount(o, id) {
		var val = $(o).val();
		if (isNaN(val)) {
			alertInfo("数量只能为数字!");
			$(o).val(0);
		}
		$.ajax({
			url : "${basePath}order/getProductCount.shtml",
			type : "post",
			dataType : "json",
			async : false,
			data : {
				productId : id
			},
			success : function(data) {
				var thisv = parseInt(val);
				if (thisv > data) {
					alertInfo("库存不足,该商品当前最大库存为" + data + ",请重新填写数量!");
					$(o).val(currentCount);
					return;
				}
				if (currentCount != null) {
					modifyGoodsStgCnt(id, currentCount - thisv, null);
				}
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});

		updateXJ(id);
		changeDiscountPrice(id);
		changeAllPrice();
		currentCount = null;
	}

	function currentc(o) {
		var v = $(o).val();
		if (v != null && v.length > 0) {
			currentCount = parseInt(v);
		}
	}

	function jian(id) {
		var v = $("#input_c_" + id).val();
		if (parseInt(v) <= 0) {
			alertInfo("数量已经为0了!");
			return;
		}
		$("#input_c_" + id).val(parseInt(v) - 1);
		modifyGoodsStgCnt(id, 1, null);
		updateXJ(id);
		changeDiscountPrice(id);
		changeAllPrice();
	}

	function jia(id) {
		var v = $("#input_c_" + id).val();
		var isOk = false;
		$.ajax({
			url : "${basePath}order/getProductCount.shtml",
			type : "post",
			dataType : "json",
			async : false,
			data : {
				productId : id
			},
			success : function(data) {
				if (parseInt(v) + 1 > data) {
					alertInfo("库存已不足!");
				} else {
					modifyGoodsStgCnt(id, -1, null);
					isOk = true;
				}
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});

		if (isOk)
			$("#input_c_" + id).val(parseInt(v) + 1);
		updateXJ(id);
		changeDiscountPrice(id);
		changeAllPrice();
	}

	function updateTotalPrice() {
		var price = 0;
		$("#table1").find("tr:not(:first,:last)").each(function() {
			var p = $(this).find("td:eq(4)").find("span").html();
			price += parseFloat(p);
		});
		$("#spzj").text(price.toFixed(2));
	}

	function updateXJ(id) {
		var tr = $("#tr_" + id);
		var num = tr.find("td:eq(2)").find("input").val();
		var price = tr.find("td:eq(3)").html();
		var xj = parseInt(num) * parseFloat(price);
		$("#span_xj_" + id).text(xj.toFixed(2));
	}

	function changeZkje() {
		var spzj = $("#spzj").html();
		if (zdzk_value != 1)
			$("#zkje").html(
					(parseFloat(spzj) - parseFloat(spzj) * zdzk_value)
							.toFixed(2));
		else
			$("#zkje").html(0);
	}

	function changeAllPrice() {
		updateTotalPrice();
		changeZkje();
		var spzj = $("#spzj").html();
		var yifje = $("#yifje").html();
		var zkje = $("#zkje").html();
		var allzhje = 0;
		$("#table1 tr:not(:first,:last)").each(function() {
			var f = $(this).find("td:eq(7)").html();
			allzhje += parseFloat(f);
		});
		allzhje = allzhje * zdzk_value;
		$("#yingfje").html(allzhje.toFixed(2));
	}

	function delPrd(id) {

		$.messager.confirm("INFO", "确认要删除吗?", function(r) {
			if (r) {
				$.ajax({
					url : "${basePath}goods/removeOrderPrd.shtml",
					type : "post",
					dataType : "json",
					data : {
						orderId : "${order.pkOrderId}",
						productId : id
					},
					success : function(data) {
						if (data) {
							$("#tr_" + id).remove();
						}
					},
					error : function(XMLHttpRequest) {
						ajaxError(XMLHttpRequest);
					}
				});
			}
		})
	}

	function subForm() {
		if (checkForm()) {
			var prds = [];
			$("#table1 tr:not(:first,:last)").each(function() {
				var id = $(this).find("td:eq(0)").html();
				var no = $(this).find("td:eq(2)").find("input:text").val();
				if (no == null)
					no = $(this).find("td:eq(2) span").text();
				var remark = $(this).find("td:eq(6)").find("input:text").val();
				var dis = discountMap.get(id);
				var prd = id;
				prd += "_" + no;
				if (dis != null) {
					prd += "_" + dis.id;
				} else {
					prd += "_" + 0;
				}
				prd += "_" + remark;

				prds.push(prd);
			});
			$("#values").val(prds.toString());
			var tre = $("#t_remark").val();
			$("#u_remark").val(tre);
			$("#AMOUNT").val($("#spzj").html());
			$("#fkDisusrId").val(manager);
			$("#updateform").submit();
		}
	}

	function checkForm() {
		var flag = true;
		$("#table1 tr:not(:first,:last)").each(function() {
			var no = $(this).find("td:eq(2)").find("input:text").val();
			if (no == null)
				no = $(this).find("td:eq(2) span").html();
			var remark = $(this).find("td:eq(6)").find("input:text").val();
			if (isNaN(no)) {
				alertInfo("商品数量只能为数字!");
				flag = false;
				return false;
			}

			if (remark.indexOf("_") > 0) {
				alertInfo("备注不允许出现特殊字符!");
				$(this).find("td:eq(6)").find("input:text")[0].focus();
				flag = false;
				return false;
			}
			if (remark.length > 25) {
				alertInfo("请控制在25个字符以内!");
				$(this).find("td:eq(6)")[0].focus();
				flag = false;
				return false;
			}
		});

		return flag;
	}

	function cancel() {
		$.messager
				.confirm(
						"提示",
						"确认要取消该订单吗",
						function(r) {
							if (r) {
								window.location.href = '${basePath}order/cancelOrder.shtml?id=${order.pkOrderId }';
							}
						});
	}

	function modifyGoodsStgCnt(id, count, suc) {
		$.ajax({
			url : "${basePath}goods/modifyGoodsStgCnt.shtml",
			type : "post",
			dataType : "json",
			data : {
				productId : id,
				count : count,
				orderId : "${order.pkOrderId }"
			},
			async : false,
			success : function(data) {
				if (suc != null)
					suc();
			},
			error : function(XMLHttpRequest) {
				ajaxError(XMLHttpRequest);
			}
		});
	}
</script>
<title>首页</title>
</head>
<body class="bg">
	<form action="${basePath }order/updateOrder.shtml" method="post"
		id="updateform">
		<t:token />
		<input type="hidden" name="pkOrderId" value="${order.pkOrderId }">
		<input type="hidden" name="values" id="values"> <input
			type="hidden" name="comments" id="u_remark"> <input
			type="hidden" name="fkDisId" id="fkDisId"> <input
			type="hidden" name="amount" id="AMOUNT"> <input type="hidden"
			name="fkDisusrId" id="fkDisusrId">

	</form>
	<input type="hidden" id="orderPrdId">
	<div class="content m_auto">
		<div class="content_l mt10">
			<ul>
				<shiro:hasPermission name="goods/list.shtml">
					<li><a href="${basePath }goods/list.shtml"><i
							class="nav_icon_k"></i><br>商品</a></li>
				</shiro:hasPermission>
				<shiro:hasPermission name="order/shopCart.shtml">
					<li class="content_hover"><a
						href="${basePath }order/shopCart.shtml?id=${order.pkOrderId}"><i
							class="nav_icon_l"></i><br>购物篮</a></li>
				</shiro:hasPermission>
				<shiro:hasPermission name="order/list.shtml">
					<li><a href="${basePath }order/list.shtml"><i
							class="nav_icon_m"></i><br>订单</a></li>
				</shiro:hasPermission>

			</ul>
		</div>
		<div class="content_r mt10">
			<div class="p20">
				<h1 class="f16">订单信息</h1>
				<div class="goods_order_top fixed mt10 f14">
					<ul class="ml10">
						<li><label>订单号：</label>${order.pkOrderId }</li>
						<li><label>客户姓名：</label>${order.operator }</li>
						<li><label>当前操作人：</label>${loginUser.name }</li>

						<li>${loginUser.branName }</li>
					</ul>
				</div>
				<table class="table mt10" id="table1">
					<tr>
						<th>商品编码</th>
						<th>商品名称</th>
						<th>数量</th>
						<th>单价</th>
						<th>小计</th>
						<th>折扣率</th>
						<th>备注</th>
						<th>折后金额</th>
						<th>操作</th>
					</tr>
					<c:if test="${empty products }">
						<tr>
							<td colspan="9" align="center"><span style="color: red;">当前还未选择任何商品，请点击按钮‘继续购物’回到购物页面</span>
							</td>
						</tr>
					</c:if>
					<c:forEach items="${products }" var="p">
						<tr id="tr_${p.fkPrdId }">
							<td>${p.fkPrdId }</td>
							<td>${p.productName }<input type="hidden"
								value="${p.allowSpedis }"></td>
							<td><c:choose>
									<c:when
										test="${p.prdCateId eq '001003' || p.prdCateId eq '001004' || !empty p.giftNo}">
								<span id="count_2_${p.fkPrdId }">${p.count }</span>
								</c:when>
									<c:otherwise>
										<a class="jia_icon" href="javascript:jian('${p.fkPrdId }')"></a>
										<input class="iput_t w50" type="text" value="${p.count }"
											id="input_c_${p.fkPrdId }" onclick="currentc(this)"
											onblur="checkCount(this,'${p.fkPrdId}')">
										<a class="jian_icon" href="javascript:jia('${p.fkPrdId }')"></a>
									</c:otherwise>
								</c:choose></td>
							<td>${p.price }</td>
							<td><span id="span_xj_${p.fkPrdId }">${p.price*p.count }</span></td>
							<td><span id="span_${p.fkPrdId }">-</span> <c:if
									test="${p.allowSpedis != '0' && empty p.giftNo }">
									<input type="button" class="btn_l" value="特殊折扣"
										onClick="openWindow('${p.fkPrdId }');">
								</c:if></td>
							<td><input type="text" class="iput_t" placeholder="备注信息"></td>
							<td>${p.price*p.count }</td>
							<td><a href="javascript:delPrd('${p.fkPrdId }');"><i
									class="delete_icon vtt"></i>删除</a></td>
						</tr>
					</c:forEach>
					<c:if test="${not empty products }">
						<tr>
							<td colspan="10">
								<ul class="goods_ul fr p10">
									<li>商品总价：<span class="color_lv mlr5 f18">￥<span
											id="spzj"></span></span></li>
									<li>已付金额：<span class="color_lv mlr5 f18">￥<span
											id="yifje">${order.paidAmount ==null?0:order.paidAmount ==null}</span></li>
									<li>整单折扣：<span class="color_lv mlr5 f18"><span
											id="zdzk">-</span></span></li>
									<li>折扣金额<span class="color_lv mlr5 f18">￥<span
											id="zkje">0</span></span></li>
									<li>应付金额<span class="fb mlr5 color_org f18">￥<span
											id="yingfje">200</span></span></li>
									<li><input type="button" onclick="zddz();" value="整单打折"
										class="btn fl"></li>
								</ul>
							</td>
						</tr>
					</c:if>
				</table>
				<textarea class="textare" placeholder="备注信息"
					style="width: 1116px; margin-top: 10px;" id="t_remark"></textarea>
				<div class="fr mt10">
					<c:choose>
						<c:when test="${order.orderStatus eq '4' }">
							<input type="button" class="btn" value="继续购物"
								onclick="javascript:window.location.href='${basePath}goods/list.shtml'">
							<c:if test="${not empty products }">
								<input type="button" class="btn ml10" value="确认"
									onclick="subForm();">
								<input type="button" class="btn ml10" value="取消"
									onClick="cancel();">
							</c:if>
						</c:when>
						<c:otherwise>
							<input type="button" class="btn ml10" value="返回"
								onClick="javascript:window.location.href='${basePath}order/list.shtml'">
						</c:otherwise>
					</c:choose>

				</div>
			</div>
		</div>
	</div>






	<!---弹出层--->
	<div id="e" class="easyui-window" title="特殊折扣设置"
		data-options="modal:true,closed:true,iconCls:'icon-save'"
		style="width: 500px; height: 300px; position: relative;">
		<div class="margin_auto w320 pt30">
			<ul class="ul_label f14">
				<li><label>折扣率：</label>
					<div class="uboxstyle">
						<select name="language_a" id="discount_select">
							<option value="">请选择</option>
							<c:forEach items="${discounts }" var="dis">
								<option value="${dis.disValue }_${dis.pkDisId}">${dis.disDesc }</option>
							</c:forEach>
						</select>
					</div></li>
				<li><label>经理密码：</label><input type="password"
					class="iput_t w200" id="mg_password"></li>
			</ul>
		</div>
		<div class="btm_btn btm_bg">
			<dl>
				<dd>
					<input type="button" class="btn" onClick="executeDiscount();"
						value="确定">
				</dd>
				<dd>
					<input type="button" class="btn" onClick="closeWindow();"
						value="取消">
				</dd>
			</dl>
		</div>
	</div>
	<!---弹出层--->

</body>
</html>